<?php
namespace app\mobile\controller;

use app\model\Record; 
use app\model\Util; 
use app\model\Theme; 

class Myorder1 extends Base
{
    public function Myorder1()
    {
global $_G, $_P;
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$system=$_G['system'];
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('活动入口不正确');
}
$recordid = intval($_P['recordid']);
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
    die(Util::showError("请确认活动地址是否正确"));
    return;
}

$config = 	Theme::getConfig($themeid);
$cachekey = 'theme'.$_G['mpid'].MODULE_NAME.'-'.$themeid;


$themyrecord = Record::getLastGroupByOpenId($_G['openid'],$themeid);
if(!empty($themyrecord)){
	if ($themyrecord['type']==1){
		$recordid = $themyrecord['pid'];
	}else{
		$recordid = $themyrecord['id'];
		$isleader = 1;
	}
	$themyrecordid = $recordid+1000;
}else{
	return_msg('未查到您的付款定单') ;
}

$showrecord = Record::getAttendGroupById($recordid);
if (empty($showrecord)){
	$showrecord = $themyrecord;
}
$detaillist = Record::getDetailListByPid($recordid);

        $count = Record::getAttendDetailNumByPId($recordid);
		if ($showrecord['renshu'] == 1) {
			$maxcount = $config['maxcountall2']-1;
		}else{
			$maxcount = $config['maxcount'];
		}	
		$nowgroupneednum=max(0,$maxcount-$count);

	    if ($showrecord['xunifinish']==1) {
	    	$kaishishu = $showrecord['uid'] % 500;
			$xunisql = "SELECT avatar,username FROM " . tablename('2019xuni_fans') . " where
				 id >". $kaishishu ." limit ".$nowgroupneednum;
			$xunilist = db_select ( $xunisql );
			if (!empty($detaillist)) {
				$detaillist = array_merge($detaillist , $xunilist);
			}else{
				$detaillist = $xunilist;
			}

	    	$nowgroupneednum=0;
	    }else{
			$num_arr = array();
			if ($config['maxcountall']<=20){
			  	for($i=0;$i<$nowgroupneednum;$i++){
			  		$num_arr[$i] = $i; 
			  	}
		  	}
	  	}

		$worker=db_find("SELECT * FROM ".tablename(t_worker)." WHERE uid = '{$themyrecord['workerid']}'");

		$shop=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id= '{$worker['company']}'");
		$shopname=$shop['name'];
		if ($config['shopstype5']==1) {
			$options = str_replace("|", "','",$themyrecord['option']);
			$tiaojian = " and name in ('" . $options ."')";
		}
		$shoplist = db_select ( "SELECT * FROM " . tablename (t_shangjia) .
				" WHERE  state = 1 " . $tiaojian . " order by displayorder" );

		if(!empty($shoplist)){
			foreach($shoplist as $k=>$item){

				$shop=db_find("SELECT addtime,workerid FROM ".tablename(t_hexiao)." WHERE uid = '{$_G['member']['uid']}' and shopid = '{$item['id']}' and themeid = '{$themeid}'");
				if (!empty($shop)) {
					if ($shop['workerid'] == '-1') {
						$shoplist[$k]['workername'] = '后台手工核销';
					}else{
						$hexiaoworker=db_find("SELECT * FROM ".tablename(t_worker)." WHERE uid = '{$shop['workerid']}'");
						$shoplist[$k]['workername'] = $hexiaoworker['username'];
						$shoplist[$k]['workermobile'] = $hexiaoworker['mobile'];
					}
					$shoplist[$k]['addtime'] = $shop['addtime'];
				}
			}
		}else{
			$shop=db_find("SELECT addtime,workerid FROM ".tablename(t_hexiao)." WHERE  uid = '{$_G['member']['uid']}' and shopid = '0' and themeid = '{$themeid}'");

			if ($shop['workerid'] == '-1') {
				$shop['workername'] = '后台手工核销';
			}else{
				$hexiaoworker=db_find("SELECT * FROM ".tablename(t_worker)." WHERE uid = '{$shop['workerid']}'");
				$shop['workername'] = $hexiaoworker['username'];
				$shop['workermobile'] = $hexiaoworker['mobile'];
			}
			$shop['addtime'] = $shop['addtime'];

		}
		$return_clicent = get_defined_vars();
return view('/yxpdd/myorder', $return_clicent);	
}
}